using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;


namespace TR0217.ControlEx
{
	internal class ColorSlider : LabelRotate
	{
		public event EventHandler SelectedValueChanged;
		Orientation _orientation = Orientation.Vertical;
		public Orientation Orientation
		{
			get { return _orientation; }
			set { _orientation = value; }
		}

		public enum eNumberOfColors
		{
			Use2Colors, 
			Use3Colors, 
		}
		eNumberOfColors _numberOfColors = eNumberOfColors.Use3Colors;
		public eNumberOfColors NumberOfColors
		{
			get { return _numberOfColors; }
			set	{ _numberOfColors = value; }
		}
		public enum eValueOrientation
		{
			MinToMax,
			MaxToMin,
		}
		eValueOrientation _valueOrientation = eValueOrientation.MinToMax;
		public eValueOrientation ValueOrientation
		{
			get { return _valueOrientation; }
			set	{ _valueOrientation = value; }
		}
		float _percent = 0;
		public float Percent
		{
			get { return _percent; }
			set
			{
				// ok so it is not really percent, but a value between 0 - 1.
				if (value < 0) value = 0;
				if (value > 1) value = 1;
				if (value != _percent)
				{
					_percent = value;
					if (SelectedValueChanged != null)
						SelectedValueChanged(this, null);
					Invalidate();
				}

			}
		}

		Color _color1 = Color.Black;
		Color _color2 = Color.FromArgb(255,127,127,127);
		Color _color3 = Color.White;
		public Color Color1
		{
			get { return _color1; }
			set { _color1 = value; }
		}
		public Color Color2
		{
			get { return _color2; }
			set { _color2 = value; }
		}
		public Color Color3
		{
			get { return _color3; }
			set { _color3 = value; }
		}

		Padding _barPadding = new Padding(12,5, 24, 10);
		public Padding BarPadding
		{
			get { return _barPadding; }
			set 
			{ 
				_barPadding = value;
				Invalidate();
			}
		}

		public ColorSlider()
		{
		}

		protected override void OnGotFocus(EventArgs e)
		{
			base.OnGotFocus(e);
			Invalidate();
		}
		protected override void OnLostFocus(EventArgs e)
		{
			base.OnLostFocus(e);
			Invalidate();
		}
		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			DrawColorBar(e.Graphics);
			
			if (Focused)
			{
				RectangleF lr = ClientRectangleF;
				lr.Inflate(-2,-2);
				ControlPaint.DrawFocusRectangle(e.Graphics, Util.Rect(lr));
			}
		}
		protected override void OnMouseMove(MouseEventArgs e)
		{
			base.OnMouseMove(e);
			PointF mousepoint = new PointF(e.X, e.Y);
			if (e.Button == MouseButtons.Left)
				SetPercent(mousepoint);
		}
		protected override void OnMouseDown(MouseEventArgs e)
		{
			base.OnMouseDown(e);
			Focus();
			PointF mousepoint = new PointF(e.X, e.Y);
			if (e.Button == MouseButtons.Left)
				SetPercent(mousepoint);
		}
		protected override bool ProcessDialogKey(Keys keyData)
		{
			float percent = Percent * 100;
			int step = 0;
			if ((keyData & Keys.Up) == Keys.Up)
				step = 1;
			if ((keyData & Keys.Down) == Keys.Down)
				step = -1;
			if ((keyData & Keys.Control) == Keys.Control)
				step *= 5;
			if (step != 0)
			{
				SetPercent((float)Math.Round(percent + step));
				return true;
			}
			return base.ProcessDialogKey(keyData);
		}
		protected virtual void SetPercent(float percent)
		{
			Percent = percent / 100;
		}
		protected virtual void SetPercent(PointF mousepoint)
		{
			RectangleF cr = ClientRectangleF;
			RectangleF br = BarRectangle;
			mousepoint.X += cr.X - br.X;
			mousepoint.Y += cr.Y - br.Y;
			Percent = GetPercentSet(BarRectangle, Orientation, mousepoint);
			Refresh();
		}
		protected RectangleF BarRectangle
		{
			get
			{
				RectangleF r = ClientRectangle;
				r.X			+= BarPadding.Left;
				r.Width		-= BarPadding.Right;
				r.Y			+= BarPadding.Top;
				r.Height	-= BarPadding.Bottom;
				return r;
			}
		}
		protected float GetPercentSet(RectangleF r, Orientation orientation, PointF mousepoint)
		{
			float percentSet = 0;
			if (orientation == Orientation.Vertical)
			{
				if (_valueOrientation == eValueOrientation.MaxToMin)
					percentSet = 1 - ((mousepoint.Y - r.Y / r.Height) / r.Height);
				else
					percentSet = mousepoint.Y / r.Height;
			}
			if (orientation == Orientation.Horizontal)
				if (_valueOrientation == eValueOrientation.MaxToMin)
					percentSet = 1 - ((mousepoint.X - r.X / r.Width) / r.Width);
				else
					percentSet = (mousepoint.X / r.Width);
			if (percentSet < 0)
				percentSet = 0;
			if (percentSet > 100)
				percentSet = 100;
			return percentSet;
		}
		protected void DrawSelector(Graphics dc, RectangleF r, Orientation orientation, float percentSet)
		{
			Pen pen = new Pen(Color.CadetBlue);
			percentSet = Math.Max(0, percentSet);
			percentSet = Math.Min(1, percentSet);
			if (orientation == Orientation.Vertical)
			{
				float selectorY = (float)Math.Floor(r.Top + (r.Height - (r.Height * percentSet)));
				if (_valueOrientation == eValueOrientation.MaxToMin)
					selectorY = (float)Math.Floor(r.Top + (r.Height - (r.Height * percentSet)));
				else
					selectorY = (float)Math.Floor(r.Top + (r.Height * percentSet));

				dc.DrawLine(pen, r.X, selectorY, r.Right, selectorY);

				Image image = SelectorImages.Image(SelectorImages.eIndexes.Right);
				float xpos = r.Right;
				float ypos = selectorY - image.Height/2;
				dc.DrawImageUnscaled(image, (int)xpos, (int)ypos);
				
				image = SelectorImages.Image(SelectorImages.eIndexes.Left);
				xpos = r.Left - image.Width;
				dc.DrawImageUnscaled(image, (int)xpos, (int)ypos);
			}
			if (orientation == Orientation.Horizontal)
			{
				float selectorX = 0;
				if (_valueOrientation == eValueOrientation.MaxToMin)
					selectorX = (float)Math.Floor(r.Left + (r.Width - (r.Width * percentSet)));
				else
					selectorX = (float)Math.Floor(r.Left + (r.Width * percentSet));

				dc.DrawLine(pen, selectorX, r.Top, selectorX, r.Bottom);

				Image image = SelectorImages.Image(SelectorImages.eIndexes.Up);
				float xpos = selectorX - image.Width/2;
				float ypos = r.Bottom;
				dc.DrawImageUnscaled(image, (int)xpos, (int)ypos);
				
				image = SelectorImages.Image(SelectorImages.eIndexes.Down);
				ypos = r.Top - image.Height;
				dc.DrawImageUnscaled(image, (int)xpos, (int)ypos);
			}
		}
		protected void DrawColorBar(Graphics dc)
		{
			RectangleF lr = BarRectangle;
			if (_numberOfColors == eNumberOfColors.Use2Colors)
				Util.Draw2ColorBar(dc, lr, Orientation, _color1, _color2);
			else
				Util.Draw3ColorBar(dc, lr, Orientation, _color1, _color2, _color3);
			DrawSelector(dc, lr, Orientation, (float)Percent);
		}
	}
}
